﻿var companyForm = {
	submitReturn : false,
	init : function(){
		$("form#buildingModel").formSetting();

		// 담당자 추가
		$("#inchargeAdd").click(companyForm.inchargeAdd);
		// 담당자 삭제
		$("#inchargeTable").on("click", ".inchargeDel", companyForm.inchargeDel);
		// 측정 추가
		$("#estimateAdd").click(companyForm.estimateAdd);
		$("#estimateTable").on("change","input.estimatSum", function(){
			if(!$(this).val()) $(this).val("0");
		}).find("input.estimatSum").each(function(){
			$(this).trigger("change");
		});
		// 측정 삭제
		$("#estimateTable").on("click", ".estimateDel", companyForm.estimateDel);

		return true;
	},

	formChk: function (){
		if(companyForm.submitReturn)return true;

		var $saveForm = $("form#buildingModel");
		if(!$saveForm.validation()) return false;

		var msg = "저장하시겠습니까?";
		if($("#buildingCd").val()){
			msg = "수정하시겠습니까?";
		}

    	companyForm.inchargeIndexSetting();
    	companyForm.estimateIndexSetting();

		common.confirm(msg, {
			"확인": function() {
	        	$( this ).dialog( "close" );
				companyForm.submitReturn = true;
				$saveForm.submit();
	        },
	        "취소": function() {
	        	$( this ).dialog( "close" );
	        }
		});
		return false;
	},

	estimateIndexSetting :function(){
		$(".item","#estimateTable").each(function(i){
	        var $item = $(this);
			var idx = $item.attr("idx");
			$item.find(":input:not(:button)").each(function () {
		        var $this = $(this);
		        var id = "";
		        if($this.attr("id")){
		        	id = $this.attr("id").replace("estimate"+idx, "estimate" + (i));
		        }
		    	var name = $this.attr("name").replace("estimate["+idx+"]", "estimate[" + (i) + "]");
		    	$this.attr("id", id).attr("name", name);
		    });

			$item.find("label").each(function () {
		        var $this = $(this);
		        var forId =$this.attr("for").replace("estimate"+idx, "estimate" + (i));
		        $this.attr("for", forId);
		    });

			$item.attr("idx", i);
		});
	},

	inchargeIndexSetting :function(){
		$(".item","#inchargeTable").each(function(i){
	        var $item = $(this);
			var idx = $item.attr("idx");
			$item.find(":input:not(:button)").each(function () {
		        var $this = $(this);
		        var id = "";
		        if($this.attr("id")){
		        	id = $this.attr("id").replace("incharge"+idx, "incharge" + (i));
		        }
		    	var name = $this.attr("name").replace("incharge["+idx+"]", "incharge[" + (i) + "]");
		    	$this.attr("id", id).attr("name", name);
		    });

			$item.find("label").each(function () {
		        var $this = $(this);
		        var forId =$this.attr("for").replace("incharge"+idx, "incharge" + (i));
		        $this.attr("for", forId);
		    });

			$item.attr("idx", i);
		});
	},

	inchargeAdd: function(){
		var $inchargeDiv = $("tbody","#inchargeTable");
		var itemNum = $inchargeDiv.find("tr.item").length;
		var $inchargeTr = $inchargeDiv.find("tr.item:eq(0)").clone()
								.attr("idx", itemNum)
								.find("[type=text]").val("").attr("value", "").end()
								.find(":hidden.key").val("").attr("value", "").end()
								.find(".tel").mask("999-9999-9999").end();

		$inchargeTr.find(":input:not(:button)").each(function () {
	        var $this = $(this);
	        var id = "";
	        if($this.attr("id")){
	        	id = $this.attr("id").replace("incharge0", "incharge" + (itemNum));
	        }
	    	var name = $this.attr("name").replace("incharge[0]", "incharge[" + (itemNum) + "]");
	    	$this.attr("id", id).attr("name", name);
	    });

		$inchargeTr.find("label").each(function () {
	        var $this = $(this);
	        var forId =$this.attr("for").replace("incharge0", "incharge" + (itemNum));
	        $this.attr("for", forId);
	    });

		$inchargeDiv.append($inchargeTr);
	},

	inchargeDel: function(){
		var itemNum = $("tbody","#inchargeTable").find("tr.item").length;

		var $tr = $(this).closest("tr");
		var $key = $tr.find(".key");
		if($key.val()) $("#inchargeDelItem").append($key.attr("name", "inchargeCd"));

		if(itemNum == 1){
			$tr
				.find("[type=text]").val("").attr("value", "").end()
				.find(":hidden.key").val("").attr("value", "").end();
		}else{
			$tr.remove();
		}
	},

	estimateAdd: function(){
		var $estimateDiv = $("tbody","#estimateTable");
		var itemNum = $estimateDiv.find("tr.item").length;
		var $estimateTr = $estimateDiv.find("tr.item:eq(0)").clone()
								.attr("idx", itemNum)
								.find("[type=text]").val("").attr("value", "").end()
								.find(":hidden.key").val("").attr("value", "").end()
								.find(".estimatSum").val("0").end()
								.find(".date").mask("9999-99-99").end();

		$estimateTr.find(":input:not(:button)").each(function () {
	        var $this = $(this);
	        var id = "";
	        if($this.attr("id")){
	        	id = $this.attr("id").replace("estimate0", "estimate" + (itemNum));
	        }
	    	var name = $this.attr("name").replace("estimate[0]", "estimate[" + (itemNum) + "]");
	    	$this.attr("id", id).attr("name", name);
	    });

		$estimateTr.find("label").each(function () {
	        var $this = $(this);
	        var forId =$this.attr("for").replace("estimate0", "estimate" + (itemNum));
	        $this.attr("for", forId);
	    });

		$estimateDiv.append($estimateTr).find("tr:last-child").find("img").remove().end().find(".date").removeClass("hasDatepicker").datepicker();
	},

	estimateDel : function(){
		var itemNum = $("tbody","#estimateTable").find("tr.item").length;
		var $tr = $(this).closest("tr");
		var $key = $tr.find(".key");
		if($key.val()) $("#estimateDelItem").append($key.attr("name", "estimateCd"));
		if(itemNum == 1){
			$tr
				.find("[type=text]").val("").attr("value", "").end()
				.find(":hidden.key").val("").attr("value", "").end()
				.find(".estimatSum").val("0").end();
		}else{
			$tr.remove();
		}
	},

	openPostcode : function(){
		var type =$(this).attr("postType");
		var $base = $(this).closest("td");
		new daum.Postcode({
            oncomplete: function(data) {
                // 팝업에서 검색결과 항목을 클릭했을때 실행할 코드를 작성하는 부분.
                // 우편번호와 주소 정보를 해당 필드에 넣고, 커서를 상세주소 필드로 이동한다.
            	if(type && type == 1){
                	$base.find(".zipcode").val(data.postcode);
            	}
            	$base.find(".addr").val(data.address).focus();
            }
        }).open();
	}
};

$(function(){
	companyForm.init();
	$(".openPostcode").click(companyForm.openPostcode);
});
